package com.xywy.find;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.widget.TitleBar2;
import com.xywy.find.bean.Remind;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * 提醒 - 提醒添加重复
 * @author xiaogd
 *
 */
public class RemindRepetActivity extends BaseActivity  {
    private static final String  TAG = "RemindRepetActivity";
    private TextView tvBack;

    private ImageView  dg1;
    private ImageView  dg2;
    private ImageView  dg3;
    private ImageView  dg4;
    private ImageView  dg5;
    private ImageView  dg6;
    private ImageView  dg7;

    private int curDayOfWeek;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //设置为全屏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.find_remind_repet);
        this.preInit();
        this.initView();
        this.loadData();
        this.setListerner();
    }
    @Override
    public void initView(){
        //title
        TitleBar2 titleBar = (TitleBar2)findViewById(R.id.find_remind_addrepet_title_bar);
        TextView tvTitle = titleBar.getTitle();
        tvBack =  titleBar.getTvBack();
        titleBar.getGo().setVisibility(View.GONE);
        titleBar.onClickBack(this);
            tvTitle.setText(getString(R.string.find_remind_repet));
            tvBack.setText(getString(R.string.find_remind_add));

        dg1 = (ImageView)findViewById(R.id.iv_find_remind_dg1);
        dg2 = (ImageView)findViewById(R.id.iv_find_remind_dg2);
        dg3 = (ImageView)findViewById(R.id.iv_find_remind_dg3);
        dg4 = (ImageView)findViewById(R.id.iv_find_remind_dg4);
        dg5 = (ImageView)findViewById(R.id.iv_find_remind_dg5);
        dg6 = (ImageView)findViewById(R.id.iv_find_remind_dg6);
        dg7 = (ImageView)findViewById(R.id.iv_find_remind_dg7);


    }

    /**
     * 事件处理
     * @param view
     */
    boolean day1=false,day2=false,day3=false,day4=false,day5=false,day6=false,day7=false;
    int [] check =new int[7];
    public void  onClickRemindAdd(View view){
        switch (view.getId()){
            case R.id.rl_find_remind_repet_day1:
                if(day1){
                    dg1.setVisibility(View.INVISIBLE);
                    day1 =false;
                    check[0] = 0;
                }else{
                    dg1.setVisibility(View.VISIBLE);
                    day1 = true;
                    check[0] = 1;
                }
               break;
            case  R.id.rl_find_remind_repet_day2:
                if(day2){
                    dg2.setVisibility(View.INVISIBLE);
                    day2 =false;
                    check[1] = 0;
                }else{
                    dg2.setVisibility(View.VISIBLE);
                    day2 = true;
                    check[1] = 1;
                }
                break;
            case  R.id.rl_find_remind_repet_day3:
                if(day3){
                    dg3.setVisibility(View.INVISIBLE);
                    day3 =false;
                    check[2] = 0;
                }else{
                    dg3.setVisibility(View.VISIBLE);
                    day3 = true;
                    check[2] = 1;
                }
                break;
            case  R.id.rl_find_remind_repet_day4:
                if(day4){
                    dg4.setVisibility(View.INVISIBLE);
                    day4 =false;
                    check[3] = 0;
                }else{
                    dg4.setVisibility(View.VISIBLE);
                    day4 = true;
                    check[3] = 1;
                }
                break;
            case  R.id.rl_find_remind_repet_day5:
                if(day5){
                    dg5.setVisibility(View.INVISIBLE);
                    day5 =false;
                    check[4] = 0;
                }else{
                    dg5.setVisibility(View.VISIBLE);
                    day5 = true;
                    check[4] = 1;
                }
                break;
            case  R.id.rl_find_remind_repet_day6:
                if(day6){
                    dg6.setVisibility(View.INVISIBLE);
                    day6 =false;
                    check[5] = 0;
                }else{
                    dg6.setVisibility(View.VISIBLE);
                    day6 = true;
                    check[5] = 1;
                }
                break;
            case  R.id.rl_find_remind_repet_day7:
                if(day7){
                    dg7.setVisibility(View.INVISIBLE);
                    day7 =false;
                    check[6] = 0;
                }else{
                    dg7.setVisibility(View.VISIBLE);
                    day7 = true;
                    check[6] = 1;
                }
                break;
        };
    }

    private void  clearDg(){
        dg1.setVisibility(View.INVISIBLE);
        dg2.setVisibility(View.INVISIBLE);
        dg3.setVisibility(View.INVISIBLE);
        dg4.setVisibility(View.INVISIBLE);
        dg5.setVisibility(View.INVISIBLE);
        dg6.setVisibility(View.INVISIBLE);
        dg7.setVisibility(View.INVISIBLE);
    }

    /**
     * 默认显示当前星期几
     */
    private void showCurrent(){
    	Calendar c = Calendar.getInstance();
        curDayOfWeek= c.get(Calendar.DAY_OF_WEEK);
        clearDg();
        switch (curDayOfWeek){
            case  1:
                dg1.setVisibility(View.VISIBLE);
                check[0] = 1;
                break;
            case  2:
                dg2.setVisibility(View.VISIBLE);
                check[1] = 1;
                break;
            case  3:
                dg3.setVisibility(View.VISIBLE);
                check[2] = 1;
                break;
            case  4:
                dg4.setVisibility(View.VISIBLE);
                check[3] = 1;
                break;
            case  5:
                dg5.setVisibility(View.VISIBLE);
                check[4] = 1;
                break;
            case  6:
                dg6.setVisibility(View.VISIBLE);
                check[5] = 1;
                break;
            case  7:
                dg7.setVisibility(View.VISIBLE);
                check[6] = 1;
                break;
        }
    }
    private Remind  remindType;
    @Override
    public void preInit() {
        curDayOfWeek= Calendar.DAY_OF_WEEK;
        Intent  intent =getIntent();
        remindType = (Remind) intent.getSerializableExtra("remindType");
    }

    @Override
    public void loadData() {
        this.showCurrent();  //默认显示
    }

    private void setListerner(){
        tvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                back();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        back();
    }

    private void  back(){
        //判断 哪些天被选中
    	ArrayList<Integer> list = new ArrayList<Integer>();
    	for(int i=0;i<check.length;i++){
    		if(check[i] ==1)
    			list.add(i);
    	}
    	int [] temp = new int[list.size()];
    	for(int i=0;i<list.size();i++){
    		temp[i] =list.get(i);
    	}
    	 remindType = (Remind) getIntent().getSerializableExtra("remindType");
    	if(remindType == null){
    		remindType = new Remind();
    	}
    	remindType.setLooparray(temp);
        Intent intent =new Intent(RemindRepetActivity.this,RemindAddActivity.class);
        intent.putExtra("weekofday",check);
        intent.putExtra("remindType",remindType);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        this.finish();
    }
}



